import json
import requests


def post(url, data=None):
    data = json.dumps(data, ensure_ascii=False)
    data = data.encode(encoding="utf-8")
    r = requests.post(url=url, data=data)
    r = json.loads(r.text)
    return r


if __name__ == "__main__":
    # 会话id，此处简单设为0
    conversation_id = 0
    
    # 1. 发送消息
    url = "http://localhost:5005/conversations/{}/messages".format(conversation_id)
    data = {
        "text": "你叫什么名字",
        "sender": "user"
    }
    result = post(url, data)
    print(result)

    # 2. 预测下一步动作
    url = "http://localhost:5005/conversations/{}/predict".format(conversation_id)
    result = post(url)
    print(result)

    # 3. 执行动作
    url = "http://localhost:5005/conversations/{}/execute".format(conversation_id)
    data = {
        "name": result["scores"][0]["action"]  # 取置信度最高的动作
    }
    result = post(url, data)
    print(result)
    print(result["messages"])  # 获取对话信息
    # [{'recipient_id': '1', 'text': 'Hey! How are you?'}]